% CC-MLI paired recording
clear all
path1='C:\Users\Tom�s\Desktop\Temp\'

dir_to_search=[path1]
formatpattern = fullfile(dir_to_search, '/*.ibw');
dInfoNS = dir(formatpattern);
[~, idx] =sort([dInfoNS.datenum]); % sort structure indices by date of creation, to avoid 10 coming before 2.

dinfo=dInfoNS(idx); % turn the not sorted structure to a sorted one.

trials=numel(dinfo)
spikeIndex=[];
%% 
for i=1:numel(dinfo)
   temp=IBWread([dinfo(i).folder '\' dinfo(i).name]);
   
   if i==1
       figure;plot(temp.y(1:1e5)*-1)
       prompt = 'set threshold for pk detection         ';
       thresh = input(prompt)
       close gcf
   else
   end
   [pks,locs]=findpeaks(temp.y*-1,'minPeakprominence',thresh,'minpeakDistance',100);
    spikeIndex=[spikeIndex;locs];
end


test=psth(spikeIndex,500,(1/temp.dx),trials,temp.Nsam);
xlabel('Time (ms)');
ylabel('Firing rate (Spikes/s)'); % (spikes/s, 500ms bin)');
set(gca,'box','off')
set(gcf,'color','white')
set(gca,'fontsize',20)
% vline(5e3,'--r')
axis tight
vline([5000 10000],'r--')

fclose all